<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="cateData" stripe style="width: 100%">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <!-- 在列中 可以通过插槽的形式 自定义这一列要渲染成什么样子 -->
          <template v-slot="obj">
            <el-button type="primary" size="mini" @click="showEditDialog(obj.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加分类的对话框 -->
      <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @closed="onAddClosed">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="addVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="addCate">添 加</el-button>
        </span>
      </el-dialog>
      <!-- 修改文章分类的对话框 -->
      <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input> </el-form-item
          ><el-form-item label="分类别称" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input> </el-form-item
        ></el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="editVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="editCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'ArtCate',
  created() {
    this.initArtCate()
  },
  data() {
    return {
      cateData: [],
      // 控制增加分类对话框的显示与隐藏
      addVisible: false,
      // 添加文章数据
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加文章规则对象
      addFormRules: {
        cate_name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称是1-10的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // 控制编辑表单的显示与隐藏
      editVisible: false,
      // 编辑表单数据对象
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑表单数据规则对象
      editFormRules: {
        cate_name: [],
        cate_alias: []
      }
    }
  },
  methods: {
    // 初始化文章列表
    async initArtCate() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return this.$message.error('获取文章列表失败')
      // this.$message.error('获取文章列表失败')
      this.cateData = res.data
    },
    // 关闭的时候把数据重置
    onAddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 实现添加文章的功能
    addCate() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 成功 发请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加文章分类失败')
        this.$message.success('添加文章分类成功')
        // 关闭对话框
        this.addVisible = false
        // 更新文章列表
        this.initArtCate()
      })
    },
    // 展示修改文章分类的对话框
    showEditDialog(row) {
      if (row.id === 1 || row.id === 2) {
        this.$message.error('管理员不允许修改此项分类')
        return
      }
      this.editForm = Object.assign({}, row)
      // 展示修改文章分类的对话框
      this.editVisible = true
    },
    // 修改文章分类
    editCate() {
      // 预校验
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error('更新失败')
        this.$message.success('更新成功')
        // 关闭对话框
        this.editVisible = false
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) {
        this.$message.error('管理员不允许删除此项分类')
        return
      }
      // 弹出层的消息提示
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      // 发请求
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (res.code !== 0) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      // 更新列表
      this.initArtCate()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
